package com.oodso.sell.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.oodso.sell.R;
import com.oodso.sell.utils.EmptyUtils;
import com.oodso.sell.utils.FrescoUtils;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * 自定义仿制ActionBar顶部样式
 *
 * @author GaiQS E-mail:gaiqs@sina.com
 * @Date 2015年3月2日
 * @Time 上午10:44:27
 */
public class ActionBar extends RelativeLayout {
    private TextView titleTextView;
    private ImageView titleImageView;
    private TextView rightTextView;
    private TextView right2TextView;
    private ImageView rightImageView;
    private SimpleDraweeView rightUserView;
    private SimpleDraweeView leftUserView;
    private ImageView leftImageView;
    private TextView leftTextView;// 左边文字
    private TextView leftView;// 左边文字加图片
    private int SIZE_MAX = 18;
    private int SIZE_RIGHT = 15;
    private int padding;

    public ActionBar(Context context) {
        this(context, null);
    }

    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttribute();
    }

    private void initAttribute() {
        padding = AutoUtils.getPercentWidthSize(34);
        addTitleView(R.string.app_name);
        setBackgroundResource(R.color.beijing);
//        setTitleViewVisibility(false);
    }

    public void setBackground(int color) {
        this.setBackgroundColor(getContext().getResources().getColor(color));
    }

    /**
     * 添加图片按钮
     *
     * @param id
     * @param resid
     * @return
     */
    private ImageView addImageViewMenu(int id, final int resid) {
        return setImageViewMenu(id, resid, padding, 0, padding, 0);
    }

    /**
     * 添加头像
     *
     * @param id
     * @return
     */
    private SimpleDraweeView addUserViewMenu(int id, String url) {
        return setUserViewMenu(id, url, 0, 0, 0, 0);
    }

    private SimpleDraweeView setUserViewMenu(int id, String url, int leftPadding, int topPadding, int rightPadding, int bottomPadding) {
        SimpleDraweeView draweeView = new SimpleDraweeView(getContext());
        draweeView.setId(id);
        draweeView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        draweeView.getHierarchy().setPlaceholderImage(R.drawable.icon_default_shop);
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setRoundAsCircle(true);
        draweeView.getHierarchy().setRoundingParams(roundingParams);
        FrescoUtils.loadImage(url, draweeView);
        return draweeView;
    }

    private ImageView setImageViewMenu(int id, int resid, int leftPadding, int topPadding, int rightPadding, int bottomPadding) {
        ImageView imageView = new ImageView(getContext());
        imageView.setId(id);
        imageView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        imageView.setImageResource(resid);
        // imageView.setBackgroundResource(R.drawable.btn_action_green);
        // 点击背景效果
        // imageView.setBackgroundResource(R.drawable.btn_action_blue);
        return imageView;
    }

    /**
     * 添加左右文字按钮，或者为按钮上下左右添加图片
     *
     * @param id       点击事件id
     * @param resid    文字资源
     * @param size     文字大小
     * @param imgResId 文字左边资源
     * @return
     */
    public TextView addTextViewMenu(int id, final int resid, int size, int imgResId) {
        return setTextViewMenu(id, resid, padding, 0, padding, 0, size, imgResId);
    }

    /**
     * 添加左右文字按钮，或者为按钮上下左右添加图片
     *
     * @param id       点击事件id
     * @param resid    文字资源
     * @param size     文字大小
     * @param imgResId 文字左边资源
     * @return
     */
    private TextView addTextView2Menu(int id, final int resid, int size, int imgResId) {
        return setTextViewMenu(id, resid, padding, 0, 0, 0, size, imgResId);
    }

    /**
     * 添加标题使用
     */
    private TextView addTitleView(int id, final int resid, int size, int imgResId) {
        return setTextViewMenu(id, resid, 0, 0, 0, 0, size, imgResId);
    }

    private TextView setTextViewMenu(int id, int resid, int leftPadding, int topPadding, int rightPadding, int bottomPadding, int size, int imgResId) {
        TextView textView = new TextView(getContext());
        textView.setLines(1);
//        textView.setMaxEms(5);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        if (-1 != id) {
            textView.setId(id);
            // 设置点击效果色值
            // textView.setBackgroundResource(R.drawable.btn_action_blue);
        }
        if (-1 != imgResId) {
            Drawable drawable = getResources().getDrawable(imgResId);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            textView.setCompoundDrawables(drawable, null, null, null);
            textView.setCompoundDrawablePadding((int) (leftPadding * 1.2));
            textView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        } else {
            textView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        }
        textView.setText(resid);
        textView.setTextSize(size);
        textView.setTextColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    /**
     * 设置左边文字点击事件
     */
    public void setLeftTextListenner(OnClickListener clickListener) {
        findViewById(R.id.actionbar_left_text_click).setOnClickListener(clickListener);
    }

    /**
     * 设置左边文字点击事件
     */
    public void setLeftUserViewListenner(OnClickListener clickListener) {
        findViewById(R.id.actionbar_left_user_click).setOnClickListener(clickListener);
    }

    /**
     * 设置左边view点击事件
     */
    public void setLeftViewListener(OnClickListener clickListener) {
        findViewById(R.id.actionbar_left_view_click).setOnClickListener(clickListener);
    }

    /**
     * 设置右边文字点击事件
     *
     * @param clickListener
     */
    public void setRightTextListener(OnClickListener clickListener) {
        findViewById(R.id.actionbar_right_text_click).setOnClickListener(clickListener);
    }

    /**
     * 设置右边文字点击事件
     *
     * @param clickListener
     */
    public void setRight2TextListener(OnClickListener clickListener) {
        findViewById(R.id.actionbar_right2_text_click).setOnClickListener(clickListener);
    }

    /**
     * 设置右边view点击事件
     */
    public void setRightViewListener(OnClickListener clickListener) {
        findViewById(R.id.actionbar_right_view_click).setOnClickListener(clickListener);
    }

    /**
     * 设置右边头像点击事件
     */
    public void setRightUserListener(OnClickListener clickListener) {
        findViewById(R.id.actionbar_right_user_click).setOnClickListener(clickListener);
    }

    /**
     * 添加控件，并设置控件在父控件的位置
     *
     * @param view
     * @param rule
     */
    private void addLocalView(View view, int rule) {
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        params.addRule(rule);
        addView(view, params);
    }

    /**
     * 添加控件，并设置控件在父控件的位置
     *
     * @param view
     * @param rule
     */
    private void addLocalView(View view, int rule, int w, int h) {
        LayoutParams params = new LayoutParams(w, h);
        params.setMargins(padding, 0, padding, 0);
        params.addRule(rule);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        addView(view, params);
    }

    private void addLocalViews(View view, int rule) {
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        params.setMargins(0, 0, 0, 0);

        if (EmptyUtils.isEmpty(rightTextView)) {
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        params.addRule(LEFT_OF, rule);
        addView(view, params);
    }

    /**
     * 动态添加中心标题 textview
     */
    public void addTitleView(int resId) {
        addLocalView(titleTextView = addTitleView(-1, resId, SIZE_MAX, -1), CENTER_IN_PARENT);
    }
    /**
     * 动态添加中心标题 textview
     */
    public void addTitleView(int resId,int size) {
        addLocalView(titleTextView = addTitleView(-1, resId, size, -1), CENTER_IN_PARENT);
    }

    public String getTitleText() {
        return titleTextView.getText().toString();
    }

    public TextView getTitleTextView() {
        return titleTextView;
    }

    public void setTitleText(int resid) {
        String text = getResources().getString(resid);
        if(text.length()>0&&text.length()<=10){
            titleTextView.setTextSize(20);
        } else if(text.length()>10&&text.length()<12){
            titleTextView.setTextSize(19);
        }else if(text.length()>=12&&text.length()<14){
            titleTextView.setTextSize(18);
        }else if(text.length()>=14&&text.length()<16){
            titleTextView.setTextSize(16);
        }else if(text.length()>=16&&text.length()<20){
            titleTextView.setTextSize(14);
        }
        titleTextView.setText(resid);

    }

    public void setTitleText(String text) {
        if(!EmptyUtils.isEmpty(text)){
            if(text.length()>0&&text.length()<=10){
                titleTextView.setTextSize(20);
            } else if(text.length()>10&&text.length()<12){
                titleTextView.setTextSize(19);
            }else if(text.length()>=12&&text.length()<14){
                text = text.substring(0,12)+"...";
                titleTextView.setTextSize(18);
            }else if(text.length()>=14&&text.length()<16){
                text = text.substring(0,12)+"...";

                titleTextView.setTextSize(16);
            }else if(text.length()>=16&&text.length()<20){
                text = text.substring(0,12)+"...";

                titleTextView.setTextSize(14);
            }else{
                text = text.substring(0,12)+"...";
                titleTextView.setTextSize(14);
            }
        }
        titleTextView.setText(text);
    }

    /**
     * 动态添加中心标题 imageView
     */
    public void addTitleImageView(int resId) {
        addLocalView(titleImageView = addImageViewMenu(-1, resId), CENTER_IN_PARENT);
    }

    public void setTitleViewVisibility(Boolean b) {
        titleTextView.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    /**
     * 向左添加文字
     *
     * @param resid
     */
    public void addLeftTextView(int resid) {
        addLocalView(leftTextView = addTextViewMenu(R.id.actionbar_left_text_click, resid, SIZE_RIGHT, -1), ALIGN_PARENT_LEFT);
    }

    /**
     * 向左添加文字
     *
     * @param resid
     */
    public void addLeftTitle(int resid) {
        addLocalView(leftTextView = addTextViewMenu(R.id.actionbar_left_text_click, resid, SIZE_MAX, -1), ALIGN_PARENT_LEFT);
    }

    public void setLeftTextVisibility(Boolean visibility) {
        leftTextView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    /**
     * 向左添加文字和图片
     *
     * @param resid
     * @param imgResId
     */
    public void addLeftTextView(int resid, int imgResId) {
        addLocalView(leftView = addTextViewMenu(R.id.actionbar_left_view_click, resid, SIZE_MAX, imgResId), ALIGN_PARENT_LEFT|CENTER_VERTICAL);
    }

    public void setLeftViewVisibility(Boolean b) {
        leftView.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    public String getLeftViewText() {
        return leftView.getText().toString();
    }

    public void setLeftViewText(int resId) {
        leftView.setText(resId);
    }

    /**
     * 设置左边图片和文字
     *
     * @param resId
     * @param imgResId
     */
    public void setLeftViewChange(int resId, int imgResId) {
        Drawable drawable = getResources().getDrawable(imgResId);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        leftView.setCompoundDrawables(drawable, null, null, null);
        leftView.setText(resId);
    }

    /**
     * 动态向左添加图片
     *
     * @param resid
     */
    public void addLeftImageView(int resid) {
        addLocalView(leftImageView = addImageViewMenu(R.id.actionbar_left_view_click, resid), ALIGN_PARENT_LEFT);
    }

    public void setLeftImageVisibility(Boolean visibility) {
        leftImageView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    /**
     * 动态改变左边图片样式
     *
     * @param level
     */
    public void setLeftImageDrawable(int level) {
        leftImageView.getDrawable();
        leftImageView.setImageLevel(level);
    }

    /**
     * 动态改变右边图片样式
     *
     * @param level
     */
    public void setRightImageDrawable(int level) {
        rightImageView.setImageResource(level);
    }

    /**
     * 动态向右边添加图片
     *
     * @param resid
     */
    public void addRightImageView(int resid) {
        addLocalView(rightImageView = addImageViewMenu(R.id.actionbar_right_view_click, resid), ALIGN_PARENT_RIGHT);
    }

    public void setRightImageListenter(OnClickListener clickListener) {
        findViewById(R.id.actionbar_right_view_click).setOnClickListener(clickListener);
    }

    /**
     * 动态向右边信息
     *
     * @param url
     */
    public void addRightUserView(String url) {
        addLocalView(rightUserView = addUserViewMenu(R.id.actionbar_right_user_click, url), RelativeLayout.CENTER_VERTICAL | RelativeLayout.ALIGN_PARENT_RIGHT, AutoUtils.getPercentWidthSize(75), AutoUtils.getPercentWidthSize(75));
    }


    /**
     * 动态向右边头像
     *
     * @param url
     */
    public void setRightUserView(String url) {
        FrescoUtils.loadImage(url, rightUserView);
    }


    /**
     * 动态向左边信息
     *
     * @param url
     */
    public void addLeftUserView(String url) {
        addLocalView(leftUserView = addUserViewMenu(R.id.actionbar_left_user_click, url), RelativeLayout.CENTER_VERTICAL | RelativeLayout.ALIGN_PARENT_LEFT, AutoUtils.getPercentWidthSize(75), AutoUtils.getPercentWidthSize(75));
    }

    /**
     * 动态向左边头像
     *
     * @param url
     */
    public void setLeftUserView(String url) {
        FrescoUtils.loadImage(url, leftUserView);
    }

    public void setLeftUserViewVisibility(Boolean visibility) {
        leftUserView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    public void setRightImageVisibility(Boolean visibility) {
        rightImageView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    public ImageView getRightImageView() {
        return rightImageView;
    }

    /**
     * 动态向右边添加文字
     *
     * @param resid
     */
    public void addRightTextView(int resid) {
        addLocalView(rightTextView = addTextViewMenu(R.id.actionbar_right_text_click, resid, SIZE_RIGHT, -1), ALIGN_PARENT_RIGHT);
    }
    /**
     * 在最右边的textview的   左边 添加一个textview
     * <p>
     * 需要先设置actionBar.addRightTextView(R.string.finish);//最右边textview
     * 不设置 居于最右边
     *
     * @param resid
     */
    public void addRight2TextView(int resid) {
        addLocalViews(right2TextView = addTextView2Menu(R.id.actionbar_right2_text_click, resid, SIZE_RIGHT, -1), R.id.actionbar_right_text_click);
    }

    /**
     * 动态向右边添加文字
     *
     * @param resid
     */
    public void addRightTextView(int resid,int size) {
        addLocalView(rightTextView = addTextViewMenu(R.id.actionbar_right_text_click, resid, size, -1), ALIGN_PARENT_RIGHT);
    }



    public void setRightTextVisibility(Boolean visibility) {
        rightTextView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    public void setRight2TextVisibility(Boolean visibility) {
        right2TextView.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    public String getRightText() {
        return rightTextView.getText().toString();
    }

    public void setRightText(int resid) {
        rightTextView.setText(resid);
    }

    public void setRight2Text(int resid) {
        right2TextView.setText(resid);
    }

    public void setRightText(String text) {
        rightTextView.setText(text);
    }

    public ImageView getTitleImageView() {
        return titleImageView;
    }

    public TextView getRightTextView() {
        return rightTextView;
    }


    public SimpleDraweeView getRightUserView() {
        return rightUserView;
    }

    public ImageView getLeftImageView() {
        return leftImageView;
    }

    public ImageView getLeftUserView() {
        return leftUserView;
    }

    public TextView getLeftTextView() {
        return leftTextView;
    }

    public TextView getLeftView() {
        return leftView;
    }

}
